package org.lex.dbq.model.bo;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

public class SQLMetaData {
	public static SQLMetaData newInstance(Connection c) throws SQLException {
		DatabaseMetaData md = c.getMetaData();

		SQLMetaData d = new SQLMetaData();
		d.catalogAtStart = md.isCatalogAtStart();
		d.catalogSeparator = md.getCatalogSeparator();
		d.catalogTerm = md.getCatalogTerm();
		d.identifierQuoteString = md.getIdentifierQuoteString();
		d.procedureTerm = md.getProcedureTerm();
		d.schemaTerm = md.getSchemaTerm();
		d.searchStringEscape = md.getSearchStringEscape();
		d.url = md.getURL();
		d.userName = md.getUserName();
		System.out.println("SQLMetaData=" + d);
		return d;
	}

	private String catalogSeparator = ".";
	private String catalogTerm = "database";
	private String identifierQuoteString = "'";
	private String procedureTerm = "procedure";
	private String schemaTerm = "schema";
	private String searchStringEscape = "'";
	private String url = null;
	private String userName = null;
	private boolean catalogAtStart = true;

	/**
	 * Retrieves whether a catalog appears at the start of a fully qualified
	 * table name
	 * 
	 * @return
	 */
	public boolean isCatalogAtStart() {
		return catalogAtStart;
	}

	public String getCatalogSeparator() {
		return catalogSeparator;
	}

	public String getCatalogTerm() {
		return catalogTerm;
	}

	public String getIdentifierQuoteString() {
		return identifierQuoteString;
	}

	public String getProcedureTerm() {
		return procedureTerm;
	}

	public String getSchemaTerm() {
		return schemaTerm;
	}

	public String getSearchStringEscape() {
		return searchStringEscape;
	}

	public String getUrl() {
		return url;
	}

	public String getUserName() {
		return userName;
	}

	public String toString() {
		StringBuilder buf = new StringBuilder();
		buf.append("[catalogAtStart=").append(catalogAtStart);
		buf.append(" catalogSeparator=").append(catalogSeparator);
		buf.append(" catalogTerm=").append(catalogTerm);
		buf.append(" identifierQuoteString=").append(identifierQuoteString);
		buf.append(" procedureTerm=").append(procedureTerm);
		buf.append(" schemaTerm=").append(schemaTerm);
		buf.append(" searchStringEscape=").append(searchStringEscape);
		buf.append(" url=").append(url);
		buf.append(" userName=").append(userName);
		buf.append(']');
		return buf.toString();
	}
}
